﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.DataEntity;
using Cosmetic.Logic;

namespace WebApp.Management.Product
{
	public partial class RecommendProduct : PageBase2
	{
		private ProductManager m_ProductManager			= new ProductManager();
        private RoleManager m_RoleManager = new RoleManager();
		protected void Page_Load(object sender, EventArgs e)
		{
            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }
			if (!IsPostBack)
			{
				this.BindProduct();
			}
		}

		private void BindProduct()
		{
            if (rblType.SelectedIndex == 0)
            {
                this.lvProdust.DataSource = this.m_ProductManager.GetRecommendProducts();
            }
            else if (rblType.SelectedIndex == 1)
            {
                this.lvProdust.DataSource = this.m_ProductManager.GetFocusProducts();
            }
            else
            {
                this.lvProdust.DataSource = this.m_ProductManager.GetPointProducts();
            }
			this.lvProdust.DataBind();
		}

		protected void lvProdust_ItemCommand(object sender, ListViewCommandEventArgs e)
		{
			if (e.Item.ItemType == ListViewItemType.DataItem)
			{
				if (e.CommandName.Equals("OnDelete"))
                {
                    Cosmetic.DataEntity.RecommendProduct theRecommendProduct = m_ProductManager.GetRecommendProductById(new Guid(e.CommandArgument.ToString()));

                    if (rblType.SelectedIndex == 0)
                    {
                        theRecommendProduct.Recommend = false;
                    }
                    else if (rblType.SelectedIndex == 1)
                    {
                        theRecommendProduct.Focus = false;
                    }
                    else
                    {
                        theRecommendProduct.Point = false;
                    }
                    if (!theRecommendProduct.Recommend && !theRecommendProduct.Focus && !theRecommendProduct.Point)
                    {
                        this.m_ProductManager.DeleteRecommendProduct(new Guid(e.CommandArgument.ToString()));
                    }
                    else
                    {
                        this.m_ProductManager.Update();
                    }
                    BindProduct();
                    //this.lvProdust.Items.Remove((ListViewDataItem)e.Item);
				}
			}
		}

		protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.BindProduct();
		}
	}
}
